<?php
namespace app\index\model;
use think\Db;
use think\Model;
class Members extends Model
{
    // 表名
    protected $name = 'members';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        
    ];
    public function getPhoneAttr($value){
        if(strlen($value)>10){
            return substr_replace($value,'****',3,4);
        }
    }
    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getMemberNicknameAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['nickname']??'';
    }
    

    public function getMemberAvatarAttr($value,$data){
        $avatar = (new \app\index\model\Members)::get(['member_id'=>$value??0])['avatar']??'';
        if(!empty($avatar)){
             //获取类型
            $url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $avatar;
            $url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $avatar;
            $avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:$avatar);
        }else{
            $url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public/assets/img/avatar.png';
            $url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public/assets/img/avatar.png';
            $avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:'');
        }
       return $avatar;
    }
    
    public function getSignTextAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['sign_text']??'';
    }

}
